﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GoogleMapLibrary;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            WebUserControl1.GoogleMap.Width = "500px";
            WebUserControl1.GoogleMap.Height = "500px";
            WebUserControl1.GoogleMap.MapType = GoogleMapObject.GoogleMapType.ROADMAP;// GoogleMapType.
            WebUserControl1.GoogleMap.ZoomLevel = 13;
            WebUserControl1.GoogleMap.CenterPoint = new GooglePoint(43.66675, -79.4042);

            WebUserControl1.GoogleMap.MouseClick += new GoogleMapObject.EventHandlerForGoogleMapMouse(GoogleMap_MouseClick);
            WebUserControl1.GoogleMap.MarkerClicked += new GoogleMapObject.EventHandlerForMarker(GoogleMap_MarkerClicked);

            CustomControl control = new CustomControl();
            control.position = CustomControl.PositionControl.RIGHT_BOTTOM;
            control.BackgroundColor = "white";
            control.BorderStyle = "solid";
            control.BorderWidth = "2px";
            control.Cursor = "pointer";
            control.TextAlign = "center";
            control.Title = "Click to create polygon";


            control.FontFamily = "Arial,sans-serif";
            control.FontSize = "12px";
            control.PaddingLeft = "4px";
            control.PaddingRight = "4px";
            control.InnerHTML = "Create polygon";

            control.Clicked += new CustomControl.EventHandlerForControl(control_Clicked1);

            WebUserControl1.GoogleMap.AddControl(control);

            CustomControl control1 = new CustomControl();
            control1.position = CustomControl.PositionControl.RIGHT_TOP;
            control1.BackgroundColor = "white";
            control1.BorderStyle = "solid";
            control1.BorderWidth = "2px";
            control1.Cursor = "pointer";
            control1.TextAlign = "center";
            control1.Title = "Click to create polyline";


            control1.FontFamily = "Arial,sans-serif";
            control1.FontSize = "12px";
            control1.PaddingLeft = "4px";
            control1.PaddingRight = "4px";
            control1.InnerHTML = "Create polyline";

            control1.Clicked += new CustomControl.EventHandlerForControl(control_Clicked);

            WebUserControl1.GoogleMap.AddControl(control1);

            //CustomControl control2 = new CustomControl();
            //control2.position = CustomControl.PositionControl.LEFT_TOP;
            //control2.BackgroundColor = "white";
            //control2.BorderStyle = "solid";
            //control2.BorderWidth = "2px";
            //control2.Cursor = "pointer";
            //control2.TextAlign = "center";
            //control2.Title = "Click to create style map";


            //control2.FontFamily = "Arial,sans-serif";
            //control2.FontSize = "12px";
            //control2.PaddingLeft = "4px";
            //control2.PaddingRight = "4px";
            //control2.InnerHTML = "Create style 1";

            //control2.Clicked += new CustomControl.EventHandlerForControl(control2_Clicked);

            //WebUserControl1.GoogleMap.AddControl(control2);

            //CustomControl control3 = new CustomControl();
            //control3.position = CustomControl.PositionControl.TOP_LEFT;
            //control3.BackgroundColor = "white";
            //control3.BorderStyle = "solid";
            //control3.BorderWidth = "2px";
            //control3.Cursor = "pointer";
            //control3.TextAlign = "center";
            //control3.Title = "Click to create style map";


            //control3.FontFamily = "Arial,sans-serif";
            //control3.FontSize = "12px";
            //control3.PaddingLeft = "4px";
            //control3.PaddingRight = "4px";
            //control3.InnerHTML = "Create style 2";

            //control3.Clicked += new CustomControl.EventHandlerForControl(control3_Clicked);

            //WebUserControl1.GoogleMap.AddControl(control3);


            GoogleMapLibrary.Style style2 = new GoogleMapLibrary.Style();

            style2.FeatureType = GoogleMapLibrary.Style.MapTypeStyleFeatureType.poi_park;
            style2.hue = "#ff0022";
            style2.saturation = 40;
            WebUserControl1.GoogleMap.AddStyle(style2);
        }
    }
    //void control2_Clicked()
    //{
    //    GoogleMapLibrary.Style style1 = new GoogleMapLibrary.Style();

    //    style1.FeatureType = GoogleMapLibrary.Style.MapTypeStyleFeatureType.all;
    //    style1.saturation = -80;

    //    WebUserControl1.GoogleMap.AddStyle(style1);

    //}

    //void control3_Clicked()
    //{
    //    GoogleMapLibrary.Style style2 = new GoogleMapLibrary.Style();

    //    style2.FeatureType = GoogleMapLibrary.Style.MapTypeStyleFeatureType.poi_park;
    //    style2.hue = "#ff0022";
    //    style2.saturation = 40;

    //    WebUserControl1.GoogleMap.AddStyle(style2);
    //}

    void control_Clicked()
    {
        //throw new NotImplementedException();
        //WebUserControl1.GoogleMap.CenterPoint = new GooglePoint(0, 0);
        GooglePolyline p = new GooglePolyline();
        p.ColorCode = "#0000FF";
        p.Width = 5;
        GooglePoint p1 = new GooglePoint(43.66675, -79.4042);
        GooglePoint p2 = new GooglePoint(43.67875, -79.4042);
        GooglePoint p3 = new GooglePoint(43.66675, -79.4142);
        p.ArrayPoint.Add(p1);
        p.ArrayPoint.Add(p2);
        p.ArrayPoint.Add(p3);
        WebUserControl1.GoogleMap.Polylines.Add(p);
    }

    void control_Clicked1()
    {
        //throw new NotImplementedException();
        //WebUserControl1.GoogleMap.CenterPoint = new GooglePoint(0, 0);
        GooglePolygon p = new GooglePolygon();
        p.FillColor = "#0000FF";
        p.FillOpacity = 50;
        //p.Width = 5;
        GooglePoint p1 = new GooglePoint(43.64675, -79.3842);
        GooglePoint p2 = new GooglePoint(43.65875, -79.3842);
        GooglePoint p3 = new GooglePoint(43.64675, -79.3942);
        p.ArrayPoint.Add(p1);
        p.ArrayPoint.Add(p2);
        p.ArrayPoint.Add(p3);
        WebUserControl1.GoogleMap.Polygons.Add(p);
    }

    void GoogleMap_MarkerClicked(string pID)
    {
        WebUserControl1.GoogleMap.Markers[pID].InfoWindow.StatusOpened = true;
    }

    void GoogleMap_MouseClick(double latitude, double longitude)
    {
        Marker mk = new Marker();
        mk.ID = "mk1";
        mk.Position = new GooglePoint(latitude, longitude);
        mk.InfoWindow.Text = "This is marker 1";
        WebUserControl1.GoogleMap.Markers.Add(mk);
        //throw new NotImplementedException();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Marker GP = new Marker();
        GP.Address = TextBoxAddress.Text;
        GP.ID = WebUserControl1.GoogleMap.Markers.Count.ToString();
        //GeocodeAddress() function will geocode address and set Latitude and Longitude of GP(GooglePoint) to it's respected value.
        if (GP.GeocodeAddress())
        {
            //Once GP is geocoded, you can add it to google map.
            GP.InfoWindow.Text = GP.Address;
            //Set GP as center point.
            WebUserControl1.GoogleMap.CenterPoint = GP.Position.Clone();
            //Clear any existing
            WebUserControl1.GoogleMap.Markers.Clear();
            //GP.Draggable = true;
            //Add geocoded GP to GoogleMapObject
            WebUserControl1.GoogleMap.Markers.Add(GP);
            WebUserControl1.GoogleMap.ZoomLevel = 15;

        }
        else
        {
            //Show error here.
        }
    }
}